function richTextParse(data) {
  //支持标签
  //	let nameArr = ['a','abbr','b','blockquote','br','code','col','colgroup','dd','del','div','dl','dt','em','fieldset','h1','h2','h3','h4','h5','h6','hr','i','img','ins','label','legend','li','ol','p','q','span','strong','sub','sup','table','tbody','td','tfoot','th','thead','tr','ul'];
  let nameArr = ['a', 'b', 'code', 'dd', 'div', 'dl', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'img', 'label', 'li', 'ol', 'p', 'span', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul', 'br'];
  //最终结构树
  let tree = [];
  //初索引
  let index = 0;
  //匹配到的所有标签
  let tagArr = data.match(/<[^>]*>/gi);
  //记录标签索引位置
  let indexArr = [];
  //计算索引位置
  for (let i = 0; i < tagArr.length; i++) {
    //添加索引值
    indexArr.push(data.indexOf(tagArr[i]))
    //从字符数据中删除标签
    data = data.replace(tagArr[i], '')
  }

  //记录基本的信息值定义
  let cls, alt, src, width, height, step, style,link;

  let i = 0;
  //按标签个数进行循环
  while (i < tagArr.length - 1) {
    //获取基本信息
    tree[index] = sendInfoTree(i)
    index++;
  }

  return tree;

  //将元素添加到tree中
  function sendInfoTree(idx) {
    getStartInfo(tagArr[idx])
    var label = tagArr[idx].match(/<*([^> ]*)/)[1];
    //第一级分支
    var obj = {
      name: checkName(label),
      attrs: {
        class: cls + ' rich-' + label,
        style: style ||'max-width: 100%'
      },
      children: []
    }
    if(label==='a'){
      obj.attrs['data-link'] = link;
    }
    //相应的一些判断
    if (label === 'img') {
      //判断如果是img特殊标签
      obj.attrs['src'] = src + '?x-oss-process=style/720';
      obj.attrs['alt'] = alt;      
      obj.attrs['width'] = width;
      obj.attrs['height'] = height;           
    } else if (step === 0) {
      //判断是否为单闭合标签（除img外的单闭合标签）-清空内容-方便处理
      obj.children.push({
        type: 'text',
        text: ''
      });
    } else if (tagArr[idx + 1] === '</' + label + '>') {
      if (label === 'iframe') {
        //判断如果是iframe特殊标签
        var reg = /https/g;
        obj.attrs['src'] = src.replace(reg, "http");       
        obj.attrs['width'] = width;
        obj.attrs['height'] = height;
      }
      if (label === 'video') {
        //判断如果是video特殊标签
        var reg=/https/g;        
        obj.attrs['src'] = src.replace(reg,"http");        
        obj.attrs['width'] = width;
        obj.attrs['height'] = height;
      }
      //判断紧跟它的下一个标签是否为它的闭合标签
      obj.children.push({
        type: 'text',
        text: data.substring(indexArr[idx], indexArr[idx + 1])
      })
      //索引指向闭合标签
      i++;
    } else {
      //剩下的则可能这是个标签嵌套的一个标签
      //截取两个开始标签中间的内容
      if (indexArr[idx] !== indexArr[idx + 1]) {
        obj.children.push({
          type: 'text',
          text: data.substring(indexArr[idx], indexArr[idx + 1])
        })
      }
      //循环向下去找
      i++;
      while (i < tagArr.length - 1) {
        obj.children.push(sendInfoTree(i));
        //如果标签中间有文本（即索引不一致）
        if (indexArr[i - 1] !== indexArr[i]) {
          obj.children.push({
            type: 'text',
            text: data.substring(indexArr[i - 1], indexArr[i])
          })
        }
        //如果下一个是该结束的话则跳出
        if (tagArr[i] === '</' + label + '>') break;
      }
    }
    i++;
    return obj;
  }

  //获取基本信息
  function getStartInfo(str) {
    //取得一些基本信息
    cls = matchRule(str, 'class');
    style = matchStyle(str);
    src = '';
    alt = '';
    width = '';
    height = '';
    link = '';
    //如果此标签为img标签
    if (str.match(/<*([^> ]*)/)[1] === 'img') {
      src = matchRule(str, 'src');
      alt = matchRule(str, 'alt');
      width = matchRule(str, 'width');
      height = matchRule(str, 'height');
    } 
    if (str.match(/<*([^> ]*)/)[1] === 'iframe') {
      src = matchRule(str, 'src');      
      width = matchRule(str, 'width');
      height = matchRule(str, 'height');
    } 
    if (str.match(/<*([^> ]*)/)[1] === 'video') {
      src = matchRule(str, 'src');
      width = matchRule(str, 'width');
      height = matchRule(str, 'height');
    } 
    
    if (str.match(/<*([^> ]*)/)[1] === 'a') {     
      link = matchRule(str, 'data-value');
    }
    //判断是否为单闭合标签
    step = str[str.length - 2] === '/' ? 0 : 1;
  }

  //获取部分属性的值
  function matchRule(str, rule) {
    let value = '';
    let re = new RegExp(rule + '=[\'"]?([^\'"]*)');
    if (str.match(re) !== null) {
      value = str.match(re)[1];
    }
    return value;
  }
  //获取style的值
  function matchStyle(str) {
    let value = '';
    let re = new RegExp("([^\\s=]+)=(['\"\\s]?)([^'\"]+)\\2(?=\\s|$|>)");
    if (str.match(re) !== null) {
      value = str.match(re)[3];
    }
    return value;
  }
  //检查是否为支持的标签
  function checkName(str) {
    let name = 'div';
    for (let i = 0; i < nameArr.length; i++){
      if (str === nameArr[i]) {
        name = str;
        break;
      }
      if (str === "iframe") {
        name = "video";
        break;
      }
      if (str === "video") {
        name = "video";
        break;
      }
    }
    return name;
  }
}

module.exports = {
  go: richTextParse
};